package com.bawei.model_sevent.Activity.Packaging;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.bawei.model_sevent.Activity.Activity.MainActivity_main;

/**
 * 时间：2021/11/13 8:12.
 * 类名：SPUtil
 * 作用：Model_sevent
 * 作者:张羽_zy
 */
public class SPUtil {
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    public SPUtil(Context context,String goods)
    {
        preferences = context.getSharedPreferences(goods,Context.MODE_PRIVATE);
        editor = preferences.edit();
    }
    /**
     * 向sp存入指定key对应的数据
     * 其中Value可以是String，boolean，float，int，long等各种基本数据类型
     * key
     * values
     */
    public void putString(String key,String values)
    {
        editor.putString(key, values);
        editor.commit();
    }
    public void putBoolean(String key,boolean values)
    {
        editor.putBoolean(key,values);
        editor.commit();
    }
    public void putFloat(String key,float values)
    {
        editor.putFloat(key, values);
        editor.commit();
    }
    public void putInt(String key,int values)
    {
        editor.putInt(key, values);
        editor.commit();
    }
    public void putLong(String key,long values)
    {
        editor.putLong(key, values);
        editor.commit();
    }
    /**
     * 清空sp里所有的数据
     */
    public void clear()
    {
        editor.clear();
        editor.commit();
    }

    /**
     * 删除SP里知道Key对应的数据项
     * @param key
     */
    public void Remove(String key)
    {
        editor.remove(key);
        editor.commit();
    }
    /**
     * 获取sp数据里指定Key对应的Values，如果key不存在，则返回默认值defValue
     */
    public String getString(String key,String deValues)
    {
        return preferences.getString(key,deValues);
    }
    public boolean getBoolean(String key,boolean deValues)
    {
        return preferences.getBoolean(key, deValues);
    }
    public float getFloat(String key,float deValues)
    {
        return preferences.getFloat(key, deValues);
    }
    public int getInt(String key,int deValues)
    {
        return preferences.getInt(key, deValues);
    }
    public long getLong(String key,long daValues)
    {
        return preferences.getLong(key, daValues);
    }
    /**
     * 判断sp是否包含特定key的数据
     */
    public boolean contains(String key)
    {
        return preferences.contains(key);
    }
}
